﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WpfComm.Models;
using Prism;
using Prism.Mvvm;
using Prism.Commands;
using System.IO.Ports;
using WpfComm.Servise;

namespace WpfComm.ViewModels
{
    public class SerialPortSetViewModel: BindableBase
    {
        static SerialPort mySerialPort;
        private SerialServer serialServer;
        public SerialPortSetViewModel()
        {
            this.SerialPortP = new SerialPortParameter();

            this.serialPortSet = new SerialPortSet();
            ConnInit();
        }
        public DelegateCommand CmdOpenSerial { get; set; }  //串口打开命令
        public DelegateCommand CmdCloseSerial { get; set; }  //串口关闭命令
        private void ConnInit() {
            this.CmdOpenSerial = new DelegateCommand(new Action(this.cmdOpenSerial));//串口打开命令
            this.CmdCloseSerial = new DelegateCommand(new Action(this.cmdCloseSerial));//串口关闭命令
        }

        /// <summary>
        /// 串口参数
        /// </summary>
        private SerialPortParameter serialPortP;
        public SerialPortParameter SerialPortP
        {
            get { return serialPortP; }
            set { serialPortP = value; }
        }

        private SerialPortSet _serialPortSet;

        public SerialPortSet serialPortSet
        {
            get { return _serialPortSet; }
            set
            {
                _serialPortSet = value;
                this.OnPropertyChanged("serialPortSet");
            }
        }



        private string strSendTime;
        //发送计时
        public string StrSendTime
        {
            get { return strSendTime; }
            set
            {
                strSendTime = value;
                this.OnPropertyChanged("StrSendTime");
            }
        }

        private string strSaveData;
        //接收缓存
        public string StrSaveData
        {
            get { return strSaveData; }
            set
            {
                strSaveData = value;
                this.OnPropertyChanged("StrSaveData");
            }
        }

        private string strSendData;
        //发送缓存
        public string StrSendData
        {
            get { return strSendData; }
            set
            {
                strSendData = value;
                this.OnPropertyChanged("StrSendData");
            }
        }

        private Boolean blAutoSend;
        //定时发送
        public Boolean BLAutoSend
        {
            get { return blAutoSend; }
            set
            {
                blAutoSend = value;
                this.OnPropertyChanged("BLAutoSend");
            }
        }

        private int sendtime;

        public int Sendtime
        {
            get { return sendtime; }
            set
            {
                sendtime = value;
                this.OnPropertyChanged("Sendtime");
            }
        }


        private void cmdOpenSerial()
        {
            try
            {
                mySerialPort = new SerialPort(SerialPortP.PortName[serialPortSet.portname_choose]);
                mySerialPort.BaudRate = SerialPortP.BaudRate[this.serialPortSet.baudrate_choose];
                mySerialPort.Parity = (Parity)Enum.Parse(typeof(Parity), this.SerialPortP.Parity[this.serialPortSet.partity_choose]);
                mySerialPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), this.SerialPortP.StopBits[this.serialPortSet.stopbits_choose]);
                mySerialPort.DataBits = this.SerialPortP.DataBits[this.serialPortSet.databits_choose];
                mySerialPort.Handshake = Handshake.None;
                serialServer = new SerialServer(mySerialPort);
                serialServer.SerialOpen();
            }
            catch (Exception e)
            {
                System.Windows.MessageBox.Show(e.ToString() + "参数配置错误");
            }
        }

        private void cmdCloseSerial()
        {
            serialServer.SerialClose();
        }
    }
}
